package org.infodavid.common.persistence;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.persistence.impl.SqlScriptHelperImpl;

/**
 * The Class SqlScriptHelper.
 */
public abstract class SqlScriptHelper {

  /** The Constant LOGGER. */
  private static final Log LOGGER = LogFactory.getLog(SqlScriptHelper.class);

  /** The singleton. */
  protected static final SqlScriptHelper SINGLETON;

  static {
    try {
      final Constructor<SqlScriptHelperImpl> constructor =
          SqlScriptHelperImpl.class.getDeclaredConstructor();
      constructor.setAccessible(true);

      SINGLETON = constructor.newInstance();
    }
    catch (final Exception e) {
      LOGGER.fatal("Can not instantiate the " + SqlScriptHelper.class.getName(), e);

      throw new Error(e);
    }
  }

  /**
   * Gets the instance.
   * @return the factory.
   */
  public static SqlScriptHelper getInstance() {
    return SINGLETON;
  }

  /**
   * The constructor.
   */
  protected SqlScriptHelper() {
    super();
  }

  /**
   * Parses the.
   * @param source the source
   * @return the string[]
   */
  public abstract String[] parse(final String source);

  /**
   * Parses the.
   * @param in the in
   * @return the string[]
   */
  public abstract String[] parse(final Reader in);

  /**
   * Parses the.
   * @param in the in
   * @return the string[]
   */
  public abstract String[] parse(final InputStream in);

  /**
   * Execute.
   * @param conn the conn
   * @param lines the lines
   * @return the number of statements executed with success
   * @throws SQLException the SQL exception
   */
  public abstract int execute(final Connection conn, final String[] lines) throws SQLException;
}
